package bbo.handel;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import datatypes.Ware;

public class WarenCSVReader {
	ArrayList<Ware> inputList = new ArrayList<Ware>();
	
	public WarenCSVReader() {
		super();
	}

	public void readFile(String csvfilename) {
		BufferedReader br = null;
		String line = "";
		String csvSplitBy = ";";

		try {
			br = new BufferedReader(new FileReader(csvfilename));

			while ((line = br.readLine()) != null) {
				String[] 	csvData = line.split(csvSplitBy);
				
				String 		name = csvData[0];
				int 		stufe = Integer.valueOf(csvData[2]);
				String		ort = csvData[1];
				
				Ware csvWareInput = new Ware(name, stufe);

				if (inputList.contains(csvWareInput)) {
					int index = inputList.indexOf(csvWareInput);
					
					if(!inputList.get(index).containsVerkaufsort(ort)){
						inputList.get(index).addVerkaufsort(ort);
					}
				} else {
					csvWareInput.addVerkaufsort(ort);
					inputList.add(csvWareInput);
				}
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.out.println("File do not exist");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public ArrayList<Ware> getInputList() {
		return inputList;
	}
}
